package cc.mrbird.febs.api;

import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.Ret;
import cc.mrbird.febs.common.utils.MD5Util;
import cc.mrbird.febs.mall.entity.Banner;
import cc.mrbird.febs.mall.service.IBannerService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Mr.Yang
 * @CreateTime: 2022-09-08
 * @Description:
 */
@Api(tags = {"API模块"})
@Slf4j
@RequestMapping("/mall")
@RestController
@AllArgsConstructor
public class ApiBannerController extends BaseController {

    private IBannerService bannerService;

    @ApiOperation(value = "轮播图list", notes = "轮播图list")
    @PostMapping("/banner/list")
    @ResponseBody
    public Ret getAllBanners(@RequestBody Banner banner, HttpServletRequest httpRequest) {
        System.out.println(httpRequest.getHeader("sign"));
        System.out.println(httpRequest.getHeader("uuid"));
        System.out.println(httpRequest.getHeader("timestamp"));
        System.out.println(JSON.toJSONString(banner));
        System.out.println("===========================================");
        String sign = httpRequest.getHeader("sign");
        String dateParam1 = "timestamp=" + httpRequest.getHeader("timestamp");
        String dateParam12 = "uuid=" + httpRequest.getHeader("uuid");
        boolean flag = MD5Util.verify(sign, JSON.toJSONString(banner), dateParam1, dateParam12, "HELLO");
        System.out.println("验签结果：" + flag);
        return Ret.success(bannerService.findBanners(banner));
    }

    @ApiOperation(value = "轮播图list", notes = "轮播图list")
    @PostMapping("/banner/bannerList")
    @ResponseBody
    public Ret getAllBannerList() {
        return Ret.success(bannerService.findBanners(null));
    }


    @ApiOperation(value = "轮播图新增", notes = "轮播图新增")
    @PostMapping("/banner")
    @ResponseBody
    public Ret addBanner(@RequestBody Banner banner) {
        this.bannerService.createBanner(banner);
        return Ret.success();
    }

    @ApiOperation(value = "轮播图删除", notes = "轮播图删除")
    @GetMapping("/banner/delete/{id}")
    @ResponseBody
    public Ret deleteBanner(@PathVariable("id") Long id) {
        this.bannerService.removeById(id);
        return Ret.success();
    }

    @ApiOperation(value = "轮播图获取", notes = "轮播图获取")
    @GetMapping("/banner/getById/{id}")
    @ResponseBody
    public Ret getById(@PathVariable("id") Long id, HttpServletRequest httpRequest) {
        System.out.println(httpRequest.getHeader("sign"));
        System.out.println(httpRequest.getHeader("uuid"));
        System.out.println(httpRequest.getHeader("timestamp"));
        return Ret.success(this.bannerService.getById(id));
    }

    @ApiOperation(value = "轮播图修改", notes = "轮播图修改")
    @PostMapping("/banner/update")
    @ResponseBody
    public Ret updateBanner(@RequestBody Banner banner) {
        this.bannerService.updateBanner(banner);
        return Ret.success();
    }

}
